-- SPDX-License-Identifier: GPL-3.0-or-later

local v_lianzou = fk.CreateSkill {
  name = "v_lianzou",
  tags = {  },
}



v_lianzou:addEffect(fk.EventPhaseEnd, {
  --赋予摸牌型技能定义
  anim_type = "drawcard",
  --触发条件：
  --触发时机的角色为遍历到的角色；遍历到的角色具有本技能；
  --被遍历到的角色处于出牌阶段。
  --被遍历到的角色本轮使用/打出牌的点数之和>=50（打出是否算入在内待定）
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(v_lianzou.name)
    and player.phase == Player.Play
    and player:getMark("@v_lianzou_count-turn") >= 50
    and (player:getMark("v_lianzou_choice") < 3)
  end,
  on_cost = function(self, event, target, player, data)
    --确认是否发动技能。
    local room = player.room
    local choiceList = {}
    print(player:getMark("v_lianzou_choice"))
    if player:getMark("v_lianzou_choice") == 2 or player:getMark("v_lianzou_choice") == 0 then 
      table.insert(choiceList, "v_lianzou_damage")
    end
    if player:getMark("v_lianzou_choice") == 1 or player:getMark("v_lianzou_choice") == 0 then 
      table.insert(choiceList, "v_lianzou_invalid")
    end
    table.insert(choiceList, "cancel")
    local choice = room:askToChoice(player, {
      choices = choiceList,
      skill_name = v_lianzou.name,
    })
    if choice == "cancel" then
      event:setCostData(self, "cancel")
      return false
    elseif choice == "v_lianzou_damage" then
      event:setCostData(self, 1)
    elseif choice == "v_lianzou_invalid" then
      event:setCostData(self, 2)
    end
    return true
    -- if room:askForSkillInvoke(player,v_lianzou.name,data) then
    --   return true
    -- end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    --判定直到点数大于等于50
    if event == fk.EventPhaseEnd then
      if event:getCostData(self) == 1 then
        room:addPlayerMark(player, "v_lianzou_choice", 1)
        room:setPlayerMark(player, "@v_lianzou_status", "v_nodamage")
      elseif event:getCostData(self) == 2 then
        room:addPlayerMark(player, "v_lianzou_choice", 2)
        room:setPlayerMark(player, "@v_lianzou_status", "v_noinvalid")
      end
      if player:getMark("v_lianzou_choice") >= 3 then
        room:setPlayerMark(player, "@v_lianzou_status", "v_fullypower")
      end
      --这里可以后续放限定动画并给大概2500ms的延时+音效。
      -- local count = 0
      -- local dummy = Fk:cloneCard("slash")
      -- while count < 50 do
      --   local judge = {
      --     who = player,
      --     reason = v_lianzou.name,
      --     pattern = ".|1~"..(50-count).."|.",
      --   }
      --   room:judge(judge)
      --   local result = judge.card
      --   count = count + result.number
      --   if count < 50 then
      --     dummy:addSubcard(result)
      --   end
      -- end
      -- room:obtainCard(player.id, dummy, true)
    end
  end,
})

v_lianzou:addEffect(fk.CardUseFinished, {
  --刷新条件
  --（手牌结算后，手牌打出结算后（这个待定））
  -- 触发时机的角色为遍历到的角色；遍历到的角色具有本技能；
  --被遍历到的角色处于出牌阶段。
  can_refresh = function(self, event, target, player, data)
    -- if not (target == player and player:hasSkill(v_lianzou.name)) then
    --   return false
    -- end
    return target == player and player:hasSkill(v_lianzou.name)
    and player.phase == Player.Play
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    --在满足以下条件的前提下，将牌的点数加入某个mark里。
    -- 卡牌存在，为实体牌, 卡牌点数小于等于13大于0。
    local card = data
    if card.card and card.card.id > 0 and card.card.number <= 13 and card.card.number > 0 then
      room:addPlayerMark(player, "@v_lianzou_count-turn", card.card.number)
    end
  end
})

return v_lianzou